package org.springframework.boot.autoconfigure.datasource.druid;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import top.malaoshi.easy.logging.Log;
import top.malaoshi.easy.logging.LogFactory;

import javax.annotation.PostConstruct;

@Configuration
public class DruidAutoConfiguration {

    static final Log LOG = LogFactory.getLog(DruidAutoConfiguration.class);
    private static final String TYPE = "com.alibaba.druid.pool.DruidDataSource";

    @Value("${spring.datasource.type}")
    String type;
    @PostConstruct
    public void handler() {
        /*
         这里与springboot自动加载druid不同，springboot是检测到druid相关类就开始执行
         这里考虑到：有时需要手动配置druid数据库连接池，所以通过判断有 spring.datasource.type 就自动加载连接池，否则就不自动加载
         如果有此配置，说明启用druid，将配置类添加到三级缓存中
         */

        if (TYPE.equals(type)) {
            LOG.debug("启用 DruidDataSource");
//            Out.println("启用 Thymeleaf");
            try {
                new DruidConfig().handle();
            } catch (Exception e) {
                LOG.error("",e);
            }
        }

    }


}
